package com.jastar.android.test.base;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.jastar.android.test.R;
import com.jastar.android.util.Utils;

import java.util.Calendar;

/** 普通组件 */
public class NormalWidgetActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal_widget);

        RadioGroup rg = findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);

        EditText et_name = findViewById(R.id.et_name);
        et_name.addTextChangedListener(new HideTextWatcher(et_name, 10));

        findViewById(R.id.button_alert).setOnClickListener(this);
        findViewById(R.id.button_date).setOnClickListener(this);
        findViewById(R.id.button_time).setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_male) {
            Toast.makeText(this, "你选择了'男'.", Toast.LENGTH_SHORT).show();
        } else if (checkedId == R.id.rb_female) {
            Toast.makeText(this, "你选择了'女'.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_alert) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("尊敬的用户");
            builder.setMessage("你真的要卸载吗？");
            builder.setPositiveButton("残忍卸载", (dialog, which) -> Toast.makeText(this, "期望下次再见.", Toast.LENGTH_SHORT).show());
            builder.setNegativeButton("我再想想", (dialog, which) -> Toast.makeText(this, "感谢继续使用.", Toast.LENGTH_SHORT).show());
            AlertDialog dialog = builder.create();
            dialog.show();

        } else if (v.getId() == R.id.button_date) {

            Calendar calendar = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(this, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH));
            dialog.show();

        } else if (v.getId() == R.id.button_time) {

            Calendar calendar = Calendar.getInstance();
            TimePickerDialog dialog = new TimePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog, this, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
            dialog.show();

        }

    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        String desc = String.format("您选择的日期是%d年%d月%d日.", year, month + 1, dayOfMonth);
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String desc = String.format("您选择的时间是%d时%d分.", hourOfDay, minute);
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    private class HideTextWatcher implements TextWatcher {

        private EditText view;
        private int maxLength;

        public HideTextWatcher(EditText view, int maxLength) {
            this.view = view;
            this.maxLength = maxLength;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString();
            if (str.length() == maxLength) {
                Utils.hideOneInputMethod(NormalWidgetActivity.this, view);
            }
        }
    }

}